package com.dbs.donatebooksystem.Service.impl;

import cn.hutool.db.sql.Order;
import com.dbs.donatebooksystem.Service.AddressService;
import com.dbs.donatebooksystem.Service.BooksService;
import com.dbs.donatebooksystem.Service.OrderService;
import com.dbs.donatebooksystem.mapper.AddressMapper;
import com.dbs.donatebooksystem.mapper.OrderMapper;
import com.dbs.donatebooksystem.mapper.booksMapper;
import com.dbs.donatebooksystem.mapper.userMapper;
import com.dbs.donatebooksystem.pojo.*;
import lombok.Builder;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.UUID;
import java.util.concurrent.atomic.AtomicInteger;

@Service
@Slf4j
public class OrderServiceImpl implements OrderService {

    @Autowired
    private OrderMapper orderMapper;

    @Autowired
    private AddressMapper addressMapper;

    @Autowired
    private booksMapper booksMapper;

    private static final AtomicInteger dailySequence = new AtomicInteger(0);
    private static final DateTimeFormatter DATE_TIME_FORMATTER = DateTimeFormatter.ofPattern("yyyyMMddHHmm");
    @Override
    public List<Orders> getOrder() {
       List<Orders> list = orderMapper.selectOrder();
        return list;
    }

    @Override
    public void insertOrder(Ids ids) {
        Address address = addressMapper.selectByAddressId(ids.getAddressId());
        Book book = booksMapper.selectBookById(ids.getBookId());
        Orders orders = new Orders();
        orders.setAddressId(ids.getAddressId());
        orders.setCreateTime(LocalDateTime.now());
        orders.setReceiverName(address.getReceiverName());
        orders.setReceiverPhone(address.getReceiverPhone());
        orders.setBookId(ids.getBookId());
        orders.setUserId(ids.getUserId());
        orders.setStatus(1);
        orders.setOrderNumber(this.generateOrderNumber());
        orderMapper.insertOrders(orders);
    }

    @Override
    public void deleteOrder(Integer orderId) {
        orderMapper.deleteOrderById(orderId);
    }


    public String generateOrderNumber() {
        String dateTimePart = LocalDateTime.now().format(DATE_TIME_FORMATTER);
        int sequence = dailySequence.incrementAndGet();
        String sequencePart = String.format("%04d", sequence);
        String randomPart = String.valueOf(new Random().nextInt(9999));
        return dateTimePart + sequencePart + randomPart;
    }

    @Override
    public List<Orders> getOrderByUserId(Integer userId) {
        List<Orders> list = orderMapper.selectOrderByUserId(userId);
        return list;
    }

    @Override
    public Orders getById(Integer orderId) {
        return orderMapper.getById(orderId);
    }

    @Override
    public void updateOrderStatus(Integer orderId, Integer newStatus) {
        orderMapper.updateOrderStatus(orderId,newStatus);
    }

    @Override
    public List<Orders> selectOrder(Orders orders) {
        if(orders.getOrderNumber()==null && orders.getReceiverName() == null && orders.getOrderId() == 0){
            return orderMapper.selectOrder();
        }
        List<Orders> ordersList =  orderMapper.conditionSelect(orders);
        return ordersList;
    }
}
